package org.zerta.troops.server;

import javax.swing.SwingUtilities;

import org.zerta.troops.common.Game;
import org.zerta.troops.common.World;
import org.zerta.troops.network.TroopsNetworkServer;
import org.zerta.troops.utilities.StringFromFile;
import org.zerta.troops.xml.WorldProcessor;

public class TroopsServer {
	
	private TroopsNetworkServer troopsNetworkServer;
	private Game game;

	private ServerConsole console;
	
	public static void main ( String args [] ) {
		new TroopsServer();
	}
	
	public TroopsServer() {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI( ); 
            }
        });
	}
	
	private void createAndShowGUI() {
		new TroopsServerUI(this);
	}
	
	public void startGame() {
		game.startGame();
		troopsNetworkServer.startBookkeeper();
	}
	
	public void startServer( String mapFile ) {
		String mapFileReal = "resources/worlds/" + mapFile + ".xml";
		String mapString = StringFromFile.readStringFromFile(mapFileReal);
		WorldProcessor mp = new WorldProcessor();
		World world = mp.processMapInput(mapString);
		game = new Game();
		game.setMap(world);
		game.setConsole(console);

		troopsNetworkServer = new TroopsNetworkServer(game);
		troopsNetworkServer.setConsole(console);
		new Thread(troopsNetworkServer).start();
	}

	public void setConsole(ServerConsole console) {
		this.console = console;
	}

	public ServerConsole getConsole() {
		return console;
	}
	
}
